package web.servlets.Filter;

import domain.Account;
import domain.Product;
import persistence.Impl.TIMEtil;
import service.DialogService;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @projectName: petStore
 * @package: web.servlets.Filter
 * @className: dialog
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/11/4 21:27
 * @version: 1.0
 */
public class dialogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    private String state;
    private String name;
    private String categoryId;
    private String productId;
    private String itemId;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        HttpSession session = httpRequest.getSession();

        state=httpRequest.getServletPath();
        Account account=(Account) session.getAttribute("account");
        itemId =httpRequest.getParameter("itemId");
        productId=httpRequest.getParameter("productId");
        categoryId=httpRequest.getParameter("categoryId");

        if(account!=null) name=account.getUsername();
        else name="localhost";
        //diary 后期账号做好之后 选择判断存储账号还是IP
        DialogService dialogService=new DialogService();
        dialogService.insertNewDiary(name, TIMEtil.getTime(),categoryId,productId,itemId,state);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
